{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Downloaded 'Robot.pde'.\n", "Downloaded 'Hit.pde'.\n", "Downloaded 'World.pde'.\n" ] } ], "source": [ "%download http://jupyter.cs.brynmawr.edu/hub/dblank/public/CS110%20Intro%20to%20Computing/2015/Lectures/Robot.pde\n", "%download http://jupyter.cs.brynmawr.edu/hub/dblank/public/CS110%20Intro%20to%20Computing/2015/Lectures/Hit.pde\n", "%download http://jupyter.cs.brynmawr.edu/hub/dblank/public/CS110%20Intro%20to%20Computing/2015/Lectures/World.pde" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " Sketch #2:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #2 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%include Robot.pde\n", "%include Hit.pde\n", "%include World.pde\n", "\n", "Robot robot;\n", "World world;\n", "int mx, my;\n", "\n", "void mousePressed() {\n", " mx = mouseX;\n", " my = mouseY;\n", "}\n", "\n", "void mouseDragged() {\n", " robot.forward((mouseX - mx)/100.0);\n", " robot.turn(-(mouseY - my)/1000.0);\n", "}\n", "\n", "void mouseReleased() {\n", " //robot.stop();\n", " robot.state = \"stop\";\n", "}\n", "\n", "class MyRobot extends Robot {\n", " MyRobot(float x, float y, float r) {\n", " super(x, y, r);\n", " this.state = \"start\";\n", " }\n", "\n", " void brain() {\n", " PImage pic = this.takePicture();\n", " // Show pic on canvas:\n", " image(pic, 500 - 256, 250);\n", " fill(0);\n", " text(\"getIR(0): \" + nf(this.getIR(0), 1, 4), 0, 265);\n", " text(\"getIR(1): \" + nf(this.getIR(1), 1, 4), 0, 280);\n", " text(\"stalled: \" + this.stalled, 0, 295);\n", " text(\"state: \" + this.state, 0, 310);\n", " text(\"time: \" + this.time, 0, 325);\n", " float speed = 3.0;\n", " // Start of good stuff:\n", " if (this.state == \"start\") {\n", " this.forward(3);\n", " this.state = \"s1\";\n", " } else if (this.state == \"s1\") {\n", " if (this.stalled) {\n", " this.state = \"s2\";\n", " } else {\n", " this.forward(3);\n", " }\n", " } else if (this.state == \"s2\") {\n", " this.backward(3);\n", " this.state = \"s3\";\n", " } else if (this.state == \"s3\") {\n", " if (this.stalled) {\n", " this.forward(3);\n", " this.state = \"s4\";\n", " } else {\n", " this.backward(3);\n", " }\n", " } else if (this.state == \"s4\") {\n", " if (this.getIR(0) < 1) {\n", " this.state = \"s5\";\n", " } else {\n", " this.forward(3);\n", " }\n", " } else if (this.state == \"s5\") {\n", " this.stop();\n", " }\n", " }\n", "}\n", "\n", "void setup() {\n", " // Set up canvas 500, 250 for World, 128 for camera:\n", " size(500, 250 + 128);\n", " // Set rate so it doesn't overwhelm your browser:\n", " frameRate(10);\n", " // Create a simulated world:\n", " world = new World(500, 250);\n", " world.addWall(100, 0, 110, 110); \n", " world.addBox(200, 95, 210, 170, color(255, 0, 255)); \n", " world.addBox(250, 95, 260, 130, color(255, 255, 0)); \n", " world.addBox(300, 190, 310, 240, color(255, 128, 0)); \n", " // Create robot, and add to world:\n", " robot = new MyRobot(400, 100, 0);\n", " world.addRobot(robot);\n", "}\n", "\n", "void draw() {\n", " // Clear:\n", " background(255);\n", " // Run simulation for one step:\n", " world.update();\n", "}\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto Processing", "language": "processing", "name": "calysto_processing" }, "language_info": { "codemirror_mode": { "name": "text/x-java", "version": 2 }, "file_extension": ".java", "mimetype": "text/x-java", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }